<?php
/*
Plugin Name: ImageDrop
Plugin URI: http://montania.se/
Description: Drag 'n' drop your images directly into the editor
Version: 1.0 beta 3
Author: Rickard Andersson
Author URI: http://montania.se/
License: GPL2
	
	Copyright 2011  Montania System AB  (email : info@montania.se)

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 2, as 
	published by the Free Software Foundation.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

if (!class_exists('ImageDrop')) : 

/**
 * This plugin will enable you to drag and drop images into the editor
 * @author Rickard Andersson <rickard@montania.se> 
 * @copyright Montania System AB
 * @package ImageDrop
 */
class ImageDrop {

	private $plugin_name 	= "imagedrop";
	private $thumb_width 	= 59;
	private $thumb_height 	= 59;
	private $grid_size 		= 32;
		
	/**
	 * The constructor is executed when the class is instatiated and the plugin gets loaded.
	 * @return void
	 * @since 1.0
	 */
	function __construct() {

		add_action('admin_init', array($this, 'init_meta_box') );	
		add_action('wp_ajax_id_load_images', array($this, 'load_images') );
		add_action('wp_ajax_id_image_count', array($this, 'image_count') );
		add_action('wp_ajax_id_load_meta_box', array($this, 'ajax_meta_box') );

	    load_plugin_textdomain($this->plugin_name, false, "/{$this->plugin_name}/language/");
	    				
		$this->init_scripts();
		$this->init_styles();		
	}
	
	/**
	 * This function is hooked to the "wp_ajax_id_image_count" action and will return the
	 * number of images currently in the DB to detect any changes and then reload the interface
	 * @return void
	 * @since 1.0
	 * @uses $wpdb
	 */
	function image_count() {
		global $wpdb;
		
		$q = "SELECT count(1) as count
				FROM `{$wpdb->prefix}posts` p
				WHERE `post_type` = 'attachment' 
				AND post_mime_type LIKE 'image/%'
				ORDER BY p.id DESC";

		$result = $wpdb->get_results($q);
		
		die($result[0]->count);
	}
	
	/**
	 * This function is hooked to the "wp_ajax_id_load_images" action and serves pages of
	 * images to the meta box by ajax calls.
	 * @return void 
	 * @since 1.0
	 * @uses $wpdb
	 */
	function load_images() {
		global $wpdb; 
	
		$slide = is_numeric($_POST['slide']) ? $_POST['slide'] : -1;
	
		if ($slide > -1) {
			
			$limit = $slide * $this->grid_size;
			
			$q = "SELECT p.ID, p.guid
				FROM `{$wpdb->prefix}posts` p
				WHERE `post_type` = 'attachment' 
				AND post_mime_type LIKE 'image/%'
				ORDER BY p.id DESC
				LIMIT $limit,{$this->grid_size}";
			
			$result = $wpdb->get_results($q);
					
			if (count($result) > 0) {
				foreach ($result as $post) {
					$images[] = array(
						'full' => wp_get_attachment_image_src( $post->ID, 'full' ),
						'large' => wp_get_attachment_image_src( $post->ID, 'large' ),
						'medium' => wp_get_attachment_image_src( $post->ID, 'medium' ),
						'thumbnail' => wp_get_attachment_image_src( $post->ID, 'thumbnail' ),
						'id' => $post->ID
					);
				}
				$this->render_images($images);
			}
		}
	
		die();
	}
	
	/**
	 * This function is hooked to the "admin_init" action and will add the meta box to the edit page
	 * @todo Add custom post types
	 * @since 1.0
	 * @return void
	 */
	function init_meta_box() {
		add_meta_box( $this->plugin_name, __("Drag 'n' drop images", $this->plugin_name), array($this, 'meta_box'), 'post', 'side', 'high' );
		add_meta_box( $this->plugin_name, __("Drag 'n' drop images", $this->plugin_name), array($this, 'meta_box'), 'page', 'side', 'high' );		
	}
	
	/**
	 * Private function to render the image tags with all the metadata needed for the plugin to work
	 * @param array $images
	 * @since 1.0
	 * @return void
	 */
	private function render_images($images) {
		foreach ($images as $image) : ?>
	
		<img src="<?php echo $image['thumbnail'][0] ?>" 
			width="<?php echo $this->thumb_width ?>" 
			height="<?php echo $this->thumb_height ?>" 
			alt="" 
			class="imagedrop" 
			data-width="<?php echo $image['full'][1] ?>" 
			data-height="<?php echo $image['full'][2] ?>" 
			data-url="<?php echo $image['full'][0] ?>" 
			data-large-width="<?php echo $image['large'][1] ?>" 
			data-large-height="<?php echo $image['large'][2] ?>" 
			data-large-url="<?php echo $image['large'][0] ?>"
			data-medium-width="<?php echo $image['medium'][1] ?>" 
			data-medium-height="<?php echo $image['medium'][2] ?>" 
			data-medium-url="<?php echo $image['medium'][0] ?>" 
			data-thumbnail-width="<?php echo $image['thumbnail'][1] ?>" 
			data-thumbnail-height="<?php echo $image['thumbnail'][2] ?>" 
			data-thumbnail-url="<?php echo $image['thumbnail'][0] ?>"
			data-id="<?php echo $image['id'] ?>" />
	
		<?php endforeach; 		
	}
	
	/**
	 * Loading the /scripts/script.js from the plugin directory. By default the script is enqueued 
	 * in the page footer for faster page loading. 
	 * @since 1.0
	 * @return void
	 */
	private function init_scripts() {

		// Only enqueue scripts on frontend. 
		if(is_admin()) {
			// Only enqueue the script if it actually exists. 
			if (file_exists(WP_PLUGIN_DIR . "/{$this->plugin_name}/scripts/admin-script.js")) {
				wp_enqueue_script($this->plugin_name . "-admin-script", WP_PLUGIN_URL . "/{$this->plugin_name}/scripts/admin-script.js", array('jquery'), '1.0', true);
			}			
		}
	}
	
	/**
	 * Loading the stylesheet for this plugin. 
	 * @since 1.0
	 * @return void
	 */
	private function init_styles() {
				
		// Only enqueue styles on frontend and if the stylesheet actually exists. 
		if(is_admin()) {		
			// Only enqueue the script if it actually exists. 
			if (file_exists(WP_PLUGIN_DIR . "/{$this->plugin_name}/styles/admin-style.css")) {
				wp_enqueue_style($this->plugin_name . "-admin-style", WP_PLUGIN_URL . "/{$this->plugin_name}/styles/admin-style.css", array(), '1.0', 'all');
			}			
		}
	}
	
	/**
	 * Helper function to return the meta_box and then just die, used in ajax calls
	 * @since 1.0
	 * @return void
	 */
	function ajax_meta_box() {
		$this->meta_box();
		die();
	}
	
	/**
	 * This function will render the meta box used for the drag-n-drop function-
	 * @since 1.0
	 * @param int $post_id
	 * @return void
	 * @uses $wpdb
	 */
	function meta_box($post_id = 0) {
		
		global $wpdb;
		
		$q = "SELECT p.ID, p.guid
				FROM `{$wpdb->prefix}posts` p
				WHERE `post_type` = 'attachment' 
				AND post_mime_type LIKE 'image/%'
				ORDER BY p.id DESC";
		
		$result = $wpdb->get_results($q);
		
		if (count($result) > 0) {
			
			$large_w = get_option("large_size_w");
			$large_h = get_option("large_size_h");
			$large_str = sprintf("%sx%s", $large_w, $large_h);
			
			$medium_w = get_option("medium_size_w");
			$medium_h = get_option("medium_size_h");
			$medium_str = sprintf("%sx%s", $medium_w, $medium_h);
			
			$thumb_w = get_option("thumbnail_size_w");
			$thumb_h = get_option("thumbnail_size_h");
			$thumb_str = sprintf("%sx%s", $thumb_w, $thumb_h);
			
			$i = 0;
			
			foreach ($result as $post) {
				$images[] = array(
					'full' => wp_get_attachment_image_src( $post->ID, 'full' ),
					'large' => wp_get_attachment_image_src( $post->ID, 'large' ),
					'medium' => wp_get_attachment_image_src( $post->ID, 'medium' ),
					'thumbnail' => wp_get_attachment_image_src( $post->ID, 'thumbnail' ),
					'id' => $post->ID
				);
				
				if (++$i == $this->grid_size) {
					break;
				}
			}
			
			require "pages/meta_box.php";
		}
	}
}

endif; // class exists imagedrop

// Register the plugin
add_action("init", create_function('', 'new ImageDrop();'));